package com.googlecode.adaboost;

import java.util.Vector;

import com.googlecode.adaboost.classifier.WeakClassifier;
import com.googlecode.adaboost.trainer.DataElement;
import com.googlecode.adaboost.trainer.TrainingData;

/**
 * @version Create on 2012-11-11
 * @author Yinzi Chen
 */

public class AdaBoost implements AdaBoostEngine {

	private Vector<WeakClassifier> classifiers;

	public AdaBoost() {
		this.classifiers = new Vector<WeakClassifier>();
	}

	public void addWeakClassifier(WeakClassifier classifier) {
		classifiers.add(classifier);
	}

	public double run(DataElement data) {
		double ft = 0.0;
		for (int i = 0; i < classifiers.size(); ++i) {
			ft += classifiers.get(i).getRate(data);
		}
		return ft;
	}

	public double runAll(TrainingData trainingData, double[] ft) {
		int E = 0;
		for (int i = 0; i < trainingData.getDataNum(); ++i) {
			ft[i] = run(trainingData.getTrainingData().get(i));
			if ((ft[i] <= 0.0 ? -1 : 1) != trainingData.getTrainingData()
					.get(i).getOutput())
				E++;
		}

		System.out.printf("ft(xi) = {");
		for (int i = 0; i < trainingData.getDataNum(); ++i) {
			System.out.printf(" %.3f%s", ft[i],
					i == trainingData.getDataNum() - 1 ? "" : ",");
		}
		System.out.println(" }");
		System.out.printf("Et = %.3f\n", E * 1.0 / trainingData.getDataNum());
		return E * 1.0 / trainingData.getDataNum();
	}

	public void dumpClassifiers() {
		for (int i = 0; i < classifiers.size(); ++i) {
			dumpClassifier(i);
		}
	}

	public void dumpClassifier(int index) {
		if (index >= classifiers.size()) {
			return;
		}
		classifiers.get(index).dumpClassifier();
	}

	@Override
	public void save(String fileName) {
		// TODO Auto-generated method stub

	}

	@Override
	public void load(String fileName) {
		// TODO Auto-generated method stub

	}

	/*
	 * public Vector<WeakClassifier> getClassifiers() { return classifiers; }
	 * 
	 * public void setClassifiers(Vector<WeakClassifier> classifiers) {
	 * this.classifiers = classifiers; }
	 */
}
